/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

//TODO : Think about improvements
//a way to handle response an easy way, a probably usefull Net class which could handle all the common network activity
//and a big wrapper with shortener services (cf shortener class, hidden somewhere in HDD)

package martian.m4gic.services 
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import martian.m4gic.tools.Strings;
	
	public class Net
	{
		static public const BLANK:String = "_blank";
		static public const SELF:String = "_self";
		
		static private const FB:String = "http://www.facebook.com/share.php?u=%u&t=%t";
		static private const TW:String = "http://twitter.com/home?status=%s";
		static private const MS:String = "http://www.myspace.com/index.cfm?fuseaction=postto&t=%t&c=%c&u=%u&l=3";
		static private const DL:String = "http://delicious.com/save?url=%u&title=%t&share=yes&notes=%n";
		static private const GD:String = "http://is.gd/api.php?longurl=%u";
		
		public function Net() { }
		
		static public function facebook(url:String, title:String = ""):void
		{
			var str:String = FB.replace(new RegExp("%u", "g"), url);
				str = str.replace(new RegExp("%t", "g"), title);
				go(str);
		}
		
		static public function twitter(status:String):void
		{
			status = Strings.truncate(status, 140);
			go(TW.replace(new RegExp("%s", "g"), status));
		}
		
		static public function myspace(url:String, title:String = "", content:String = ""):void
		{
			var str:String = MS.replace(new RegExp("%u", "g"), url);
				str = str.replace(new RegExp("%t", "g"), title);
				str = str.replace(new RegExp("%c", "g"), content);
				go(str);
		}
		
		static public function delicious(url:String, title:String = "", notes:String = ""):void
		{
			var str:String = DL.replace(new RegExp("%u", "g"), url);
				str = str.replace(new RegExp("%t", "g"), title);
				str = str.replace(new RegExp("%n", "g"), notes);
				go(str);
		}
		
		static public function go(request:*, window:String = BLANK):void
		{
			if (request is String) { request = new URLRequest(encodeURI(request)); }
			else if (!request is URLRequest) { throw new ArgumentError("request argument can be String or URLRequest"); }

			navigateToURL(request, window);
		}
	}
}